home *** CD-ROM | disk | FTP | other *** search
- Path: winternet.com!not-for-mail
- From: jdege@winternet.com (Jeff Dege)
- Newsgroups: comp.lang.c++
- Subject: Re: Fishing for Opinions: Global Variables in GUI
- Date: 12 Jan 1996 15:49:42 GMT
- Organization: StarNet Communications, Inc
- Message-ID: <4d5vum$el4@blackice.winternet.com>
- References: <4cjg9c$m92@crchh327.rich.bnr.ca> <4d3o7o$d9m@news.bridge.net> <30f657d7.2731554@ixnews1.ix.netcom.com>
- NNTP-Posting-Host: klondike.winternet.com
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- On Fri, 12 Jan 1996 12:59:08 GMT, n4jvp (n4jvp@ix.netcom.com) wrote:
- : To avoid using globals in multi-module programs I have been
- : using the extern keyword within the specific functions needing access
- : to the globals i.e.
- : [...]
- : Is this a good practice or is there a better way to do it?
-
- Well, you're still using globals, you're just hiding access to them.
- My biggest problem with this is the separation between declaration and
- definition. You have type-safety in C and C++ _only_ if every variable
- definition is identical. The only way to ensure this is to require that
- there only be _one_ definition of a global variable. Generally, this
- is done with header files.
-
- // globals.h
- extern short TheGlobalWidgetCount;
-
- // globals.cpp
- #include "globals.h"
- short TheGlobalWidgetCount = 0;
-
- // stuff.cpp
- #include "globals.h"
- ...
- TheGlobalWidgetCount += 1;
- ...
-
- If I should change the type of TheGlobalWidgetCount from short to long,
- I need to change it in two places, globals.h and globals.cpp, and the
- compiler will complain if I don't. If I have extern declarations scattered
- all over my code, I need to change each and every one of them, and the
- compiler won't catch those I miss. The program will simply crash at runtime.
-
- --
- "I quite agree with you," said the Duchess; "and the moral of
- that is -- `Be what you would seem to be' -- or, if you'd like it put
- more simply -- `Never imagine yourself not to be otherwise than what it
- might appear to others that what you were or might have been was not
- otherwise than what you had been would have appeared to them to be
- otherwise.'"
- -- Lewis Carrol, "Alice in Wonderland"
-
-
-